package com.hczt.xhminiapp.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Damon on 2017/11/22.
 */
public class DateUtil extends DateUtils {
	/**
	 * yyyy-MM-dd
	 */
	public final static String DATE_FORMAT    = "yyyy-MM-dd";
	/**
	 * yyyy年MM月dd日
	 */
	public final static String DATE_FORMAT_CN = "yyyy年MM月dd日";
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public final static String TIME_FORMAT    = "yyyy-MM-dd HH:mm:ss";

	/**
	 * yyyy-MM-dd HH:mm
	 */
	public final static String TIME_FORMAT_MINUTE = "yyyy-MM-dd HH:mm";

	/**
	 * yyyy年MM月dd日 HH:mm:ss
	 */
	public final static String TIME_FORMAT_CN        = "yyyy年MM月dd日 HH:mm:ss";
	/**
	 * yyyy-MM
	 */
	public final static String MONTH_FORMAT          = "yyyy-MM";
	/**
	 * yyyyMMdd
	 */
	public final static String DAY_FORMAT            = "yyyyMMdd";
	/**
	 * yyyyMMddHHmmss
	 */
	public final static String TIME_FORMAT_FOR_NAME  = "yyyyMMddHHmmss";
	/**
	 * yyMMddHHmmss
	 */
	public final static String TIME_FORMAT_FOR_SHORT = "yyMMddHHmmss";

	/**
	 * yyyyMMddHHmmss
	 */
	public final static String TIME_FORMAT_TIME = "yyyyMMddHHmmss";

	public static Date formateData(Date date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date2str(date, format));
	}

	public static Date str2date(String dateStr, String format) throws ParseException {
		if (StringUtils.isEmpty(dateStr)) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(dateStr);
	}

	public static String date2str(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * date format yyyy-MM-dd
	 *
	 * @param date
	 * @return
	 */
	public static String dateToSimpleDateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		return sdf.format(date);
	}

	public static Date addDate(Date date, int addDay) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, addDay);
		return c.getTime();
	}

	public static Date dayStartTime(Date date) throws ParseException {
		String dateStr = date2str(date, DATE_FORMAT);
		return str2date(dateStr, DATE_FORMAT);
	}


	/**
	 * 获取某一天的最后一秒
	 *
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date dayEndTime(Date date) throws ParseException {
		String dateStr = date2str(date, DATE_FORMAT);
		return str2date(dateStr + " 23:59:59", TIME_FORMAT);
	}

	/**
	 * start
	 * 本周开始时间戳 - 以星期一为本周的第一天
	 */
	public static Date getThisMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0) {
			day_of_week = 7;
		}
		cal.add(Calendar.DATE, -day_of_week + 1);
		return cal.getTime();
	}

	/**
	 * 获取某时间段内所有的日期
	 *
	 * @return
	 */
	public static List<Date> findDatesBetweenTwoDate(Date beginDate, Date endDate) throws ParseException {
		List<Date> dates = new LinkedList<>();
		if (!beginDate.after(endDate)) {
			beginDate = DateUtil.formateData(beginDate, DATE_FORMAT);
			endDate = DateUtil.formateData(endDate, DATE_FORMAT);
			dates.add(beginDate);
			Calendar calBegin = Calendar.getInstance();
			calBegin.setTime(beginDate);
			Calendar calEnd = Calendar.getInstance();
			calEnd.setTime(endDate);
			while (endDate.after(calBegin.getTime())) {
				calBegin.add(Calendar.DAY_OF_MONTH, 1);
				dates.add(calBegin.getTime());
			}
		}
		return dates;
	}


	/**
	 * 获取某日期的前后的日期
	 *
	 * @param date         指定的日期
	 * @param interval     间隔的时间 正为之后的日期，负为之前的日期
	 * @param calendarUnit 间隔单位
	 * @return
	 */
	public static Date intervalOfDate(Date date, Integer interval, Integer calendarUnit) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendarUnit, calendar.get(calendarUnit) + interval);
		return calendar.getTime();
	}


	/**
	 * 判断时间是不是今天
	 * @param date
	 * @return    是返回true，不是返回false
	 */
	public static boolean isToday(Date date) {
		//当前时间
		Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		//获取今天的日期
		String nowDay = sf.format(now);

		//对比的时间
		String day = sf.format(date);
		return day.equals(nowDay);
	}

	public static String formatDateTime(String startTime, String endTime) {
		String startDate = startTime.substring(0, 10);
		String endDate = endTime.substring(0, 10);
		String beginTime = startTime.substring(11, 19);
		String overTime = endTime.substring(11, 19);
		if (startDate.equals(endDate)) {
			return startDate + " " + beginTime + " ~ " + overTime;
		} else {
			return startDate + " " + beginTime + " ~ " + endDate + " " + overTime;
		}
	}

	public static void main(String[] args) {
		Date date = DateUtil.intervalOfDate(new Date(), -30, Calendar.MINUTE);
		System.out.println(DateUtil.date2str(date, DateUtil.TIME_FORMAT));
	}

}
